package com.ruoyi.pms.service.impl;

import java.util.List;
import com.ruoyi.common.core.utils.DateUtils;
import com.ruoyi.common.core.utils.SecurityUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import java.util.ArrayList;
import com.ruoyi.common.core.utils.StringUtils;
import org.springframework.transaction.annotation.Transactional;
import com.ruoyi.pms.domain.PmsRequestDetails;
import com.ruoyi.pms.mapper.PmsRequestMapper;
import com.ruoyi.pms.domain.PmsRequest;
import com.ruoyi.pms.service.IPmsRequestService;

import javax.annotation.Resource;

/**
 * 采购申请Service业务层处理
 * 
 * @author ruoyi
 * @date 2021-11-04
 */
@Service
public class PmsRequestServiceImpl implements IPmsRequestService 
{
    @Resource
    private PmsRequestMapper pmsRequestMapper;

    /**
     * 查询采购申请
     * 
     * @param requestUuid 采购申请主键
     * @return 采购申请
     */
    @Override
    public PmsRequest selectPmsRequestByRequestUuid(String requestUuid)
    {
        return pmsRequestMapper.selectPmsRequestByRequestUuid(requestUuid);
    }

    /**
     * 查询采购申请列表
     * 
     * @param pmsRequest 采购申请
     * @return 采购申请
     */
    @Override
    public List<PmsRequest> selectPmsRequestList(PmsRequest pmsRequest)
    {
        return pmsRequestMapper.selectPmsRequestList(pmsRequest);
    }

    /**
     * 新增采购申请
     * 
     * @param pmsRequest 采购申请
     * @return 结果
     */
    @Transactional
    @Override
    public int insertPmsRequest(PmsRequest pmsRequest)
    {
        pmsRequest.setCreateBy( SecurityUtils.getUsername() );
        pmsRequest.setCreateTime(DateUtils.getNowDate());
        int rows = pmsRequestMapper.insertPmsRequest(pmsRequest);
        insertPmsRequestDetails(pmsRequest);
        return rows;
    }

    /**
     * 修改采购申请
     * 
     * @param pmsRequest 采购申请
     * @return 结果
     */
    @Transactional
    @Override
    public int updatePmsRequest(PmsRequest pmsRequest)
    {
        pmsRequest.setUpdateBy( SecurityUtils.getUsername() );
        pmsRequest.setUpdateTime(DateUtils.getNowDate());
        pmsRequestMapper.deletePmsRequestDetailsByFkRequestUuid(pmsRequest.getRequestUuid());
        insertPmsRequestDetails(pmsRequest);
        return pmsRequestMapper.updatePmsRequest(pmsRequest);
    }
    /**
     * 修改采购申请
     *
     * @param pmsRequest 采购申请
     * @return 结果
     */
    @Transactional
    @Override
    public int confirmPmsRequest(PmsRequest pmsRequest)
    {
        return pmsRequestMapper.confirmPmsRequest(pmsRequest);
    }

    /**
     * 批量删除采购申请
     * 
     * @param requestUuids 需要删除的采购申请主键
     * @return 结果
     */
    @Transactional
    @Override
    public int deletePmsRequestByRequestUuids(String[] requestUuids)
    {
        pmsRequestMapper.deletePmsRequestDetailsByFkRequestUuids(requestUuids);
        return pmsRequestMapper.deletePmsRequestByRequestUuids(requestUuids);
    }

    /**
     * 删除采购申请信息
     * 
     * @param requestUuid 采购申请主键
     * @return 结果
     */
    @Override
    public int deletePmsRequestByRequestUuid(String requestUuid)
    {
        pmsRequestMapper.deletePmsRequestDetailsByFkRequestUuid(requestUuid);
        return pmsRequestMapper.deletePmsRequestByRequestUuid(requestUuid);
    }

    /**
     * 新增采购申请信息
     * 
     * @param pmsRequest 采购申请对象
     */
    public void insertPmsRequestDetails(PmsRequest pmsRequest)
    {

        List<PmsRequestDetails> pmsRequestDetailsList = pmsRequest.getPmsRequestDetailsList();
        String requestUuid = pmsRequest.getRequestUuid();
        if (StringUtils.isNotNull(pmsRequestDetailsList))
        {
            List<PmsRequestDetails> list = new ArrayList<PmsRequestDetails>();
            for (PmsRequestDetails pmsRequestDetails : pmsRequestDetailsList)
            {
                pmsRequestDetails.setFkRequestUuid(requestUuid);
                list.add(pmsRequestDetails);
            }
            if (list.size() > 0)
            {
                pmsRequestMapper.batchPmsRequestDetails(list);
            }
        }
    }
}
